package com.show.service.grid_owner_log;

import com.show.util.FileUtil;
import com.show.util.PageUtil;
import com.show.util.QueryUtil;
import com.show.util.StringUtil;
import com.show.dao.grid_owner_log.GridOwnerLogRepository;
import com.show.dto.grid_owner.GridOwner;
import com.show.dto.grid_owner_log.GridOwnerLog;
import com.show.dto.sys_emp.SysEmp;
import com.show.dto.sys_emp.SysEmpOrg;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by 顾志杰 on 2018/2/5.
 */
@Service
@Transactional
public class GridOwnerLogService {

    final private GridOwnerLogRepository gridOwnerLogRepository;

    final private QueryUtil queryUtil;

    final private FileUtil fileUtil;

    @Autowired
    public GridOwnerLogService(GridOwnerLogRepository gridOwnerLogRepository, QueryUtil queryUtil, FileUtil fileUtil) {
        this.gridOwnerLogRepository = gridOwnerLogRepository;
        this.queryUtil = queryUtil;
        this.fileUtil = fileUtil;
    }


    /**
     * 查询日志
     *
     * @param gridOwnerLog 查询条件,page
     * @param page         页码
     * @param pageSize     页容
     * @author 顾志杰
     * @date 2018/2/5 13:27
     */
    public PageUtil<GridOwnerLog> findAll(GridOwnerLog gridOwnerLog, Integer page, Integer pageSize) {
        StringBuffer sql = new StringBuffer();
        if (null == page || "".equals(page) || "0".equals(page)) {//排空，设置默认值
            page = 1;
        }
        if (null == pageSize || "".equals(pageSize) || "0".equals(pageSize)) {//排空，设置默认值
            pageSize = 10;
        }
        sql.append("select l.*,e.emp_name empName,o.org_name orgName,g.grid_name gridName from grid_owner_log l \n" +
                "LEFT JOIN sys_emp e ON l.emp_id = e.emp_id \n" +
                "LEFT JOIN sys_org o on l.org_id = o.org_id\n" +
                "LEFT JOIN grid g on l.grid_id = g.grid_id\n" +
                "where l.sts=1");

        if (StringUtils.isNotBlank(gridOwnerLog.getOnDate())) {
            sql.append(" and l.cdate>='" + gridOwnerLog.getOnDate() + "'");
        }
        if (StringUtils.isNotBlank(gridOwnerLog.getOutDate())) {
            sql.append(" and l.cdate<='" + gridOwnerLog.getOutDate() + " 23:59:59'");
        }
        if (StringUtils.isNotBlank(gridOwnerLog.getEmpName())) {
            sql.append(" and e.emp_name like '%" + gridOwnerLog.getEmpName() + "%'");
        }
        if (gridOwnerLog.getDescription() != null) {
            sql.append(" and l.description=" + gridOwnerLog.getDescription());
        }
        sql.append(" ORDER BY l.cdate DESC ");
        PageUtil<GridOwnerLog> roleMenus = queryUtil.queryByPage(sql.toString(), GridOwnerLog.class, page, pageSize);
        return roleMenus;
    }

    /**
     * 查询个人信息
     *
     * @param
     * @author 顾志杰
     * @date 2018/2/5 14:49
     */
    public GridOwnerLog Emp(Integer empId) {
        StringBuffer sql = new StringBuffer();
        sql.append("select e.emp_id,g.org_id,.g.grid_id,e.emp_name empName,g.grid_name gridName,(SELECT org_name from sys_org where sts=1 and org_id=g.org_id) orgName from sys_emp e \n" +
                "LEFT JOIN (select o.emp_id,g.grid_name,g.org_id,g.grid_id from grid_owner o LEFT JOIN grid g on g.grid_id=o.grid_id) g ON e.emp_id=g.emp_id\n" +
                "where e.sts=1 and e.emp_id=" + empId + "");
        List<GridOwnerLog> list = queryUtil.query(sql.toString(), GridOwnerLog.class);
        GridOwnerLog gridOwnerLog = new GridOwnerLog();
        if (list.size() != 0) {
            gridOwnerLog.setEmpId(list.get(0).getEmpId());
            gridOwnerLog.setOrgId(list.get(0).getOrgId());
            gridOwnerLog.setGridId(list.get(0).getGridId());
            gridOwnerLog.setEmpName(list.get(0).getEmpName());
            gridOwnerLog.setOrgName(list.get(0).getOrgName());
            gridOwnerLog.setGridName(list.get(0).getGridName());
        }
        return gridOwnerLog;
    }

    /**
     * 查询登陆人信息
     *
     * @param
     * @author 顾志杰
     * @date 2018/2/8 10:10
     */
    public SysEmp findEmpOne(String loginName) {
        String sql = "select * from sys_emp where sts = 1 and login_name='" + loginName + "'";
        SysEmp emp = queryUtil.query(sql, SysEmp.class).get(0);
        return emp;
    }

    /**
     * 查询登陆人信息
     *
     * @param
     * @author 顾志杰
     * @date 2018/2/8 10:10
     */
    public SysEmp findEmpById(String id) {
        String sql = "select * from sys_emp where sts = 1 and emp_id=" + id;
        SysEmp emp = queryUtil.query(sql, SysEmp.class).get(0);
        return emp;
    }

    /**
     * 上报日志
     *
     * @param
     * @author 顾志杰
     * @date 2018/2/5 15:52
     */
    public String insert(GridOwnerLog gridOwnerLog, MultipartFile[] imgFile) {
        Date date = new Date();
        FileUtil fileUtil = new FileUtil();
        List list = new ArrayList();
        if (imgFile.length != 0) {
            list = fileUtil.filesUpload(imgFile);
        }
        try {
            gridOwnerLog.setCdate(date);
            gridOwnerLog.setSts(1);
            gridOwnerLog.setDescription(0);
            if (list.size() > 0) {
                gridOwnerLog.setEspPath(list.get(0).toString());
            } else {
                gridOwnerLog.setEspPath("");
            }
            gridOwnerLog.setSts(1);
            if (gridOwnerLog.getSourceId() == null) {
                gridOwnerLog.setSourceId(1);
            }
            gridOwnerLogRepository.save(gridOwnerLog);
            return "上传成功";
        } catch (Exception e) {
            return "上传失败";
        }
    }


    /**
     * 网格长日志上报
     *
     * @param empId         网格长id
     * @param gridOwnerLog  网格长日志实体类
     * @param multipartFile 文件对象
     * @param sourceId      1为电脑   0为手机
     * @author liukai
     */
    public void saveGridOwnerLog(int empId, GridOwnerLog gridOwnerLog, MultipartFile multipartFile, int sourceId) {

        int gridId = gridOwnerLogRepository.getGridId(empId);
        int orgId = gridOwnerLogRepository.getOrgId(empId,gridId);

        if (multipartFile != null) {
            if (StringUtil.isNotEmpty(multipartFile.getOriginalFilename())) {
                String fileName = fileUtil.saveFile(multipartFile);
                gridOwnerLog.setEspPath(fileName);
            }
        }
        gridOwnerLog.setCdate(new Date());
        gridOwnerLog.setSourceId(sourceId);
        gridOwnerLog.setSts(1);
        gridOwnerLog.setDescription(0);
        gridOwnerLog.setGridId(gridId);
        gridOwnerLog.setOrgId(orgId);
        gridOwnerLog.setEmpId(empId);
        gridOwnerLogRepository.save(gridOwnerLog);
    }


    /**
     * 删除日志
     *
     * @param id 日志id
     * @author 顾志杰
     * @date 2018/2/6 9:14
     */
    public String delete(Integer id) {
        try {
            GridOwnerLog gridOwnerLog = gridOwnerLogRepository.findOne(id);
            gridOwnerLog.setSts(0);
            gridOwnerLogRepository.saveAndFlush(gridOwnerLog);
            return "删除成功";
        } catch (Exception e) {
            return "删除失败";
        }
    }

    /**
     * 查询单条日志
     *
     * @param id 日志id
     * @author 顾志杰
     * @date 2018/2/6 9:17
     */
    public GridOwnerLog findOne(Integer id) {
        GridOwnerLog gridOwnerLog = new GridOwnerLog();
        gridOwnerLog = gridOwnerLogRepository.findOne(id);
        GridOwnerLog gridOwnerLog1 = Emp(gridOwnerLog.getEmpId());
        gridOwnerLog.setEmpName(gridOwnerLog1.getEmpName());
        gridOwnerLog.setOrgName(gridOwnerLog1.getOrgName());
        gridOwnerLog.setGridName(gridOwnerLog1.getGridName());
        return gridOwnerLog;
    }

    /**
     * 审核日志
     *
     * @param
     * @author 顾志杰
     * @date 2018/2/6 9:33
     */
    public String examine(GridOwnerLog gridOwnerLog) {

        try {
            GridOwnerLog gridOwnerLog1 = gridOwnerLogRepository.findOne(gridOwnerLog.getGridOwnerLogId());
            gridOwnerLog1.setDescription(gridOwnerLog.getDescription());
            gridOwnerLogRepository.saveAndFlush(gridOwnerLog1);
            return "审核成功";
        } catch (Exception e) {
            return "审核失败";
        }
    }

    //获取当前人所在社区的全部网格长日记
    public PageUtil<GridOwnerLog> gridLoglist(Integer id, Integer page, Integer pageSize) {
        String sql = "SELECT\n" +
                "	ll.*,e.emp_name AS empName\n" +
                "FROM\n" +
                "	grid_owner_log ll\n" +
                "LEFT JOIN sys_org o ON ll.org_id = o.org_id\n" +
                "LEFT JOIN sys_emp_org eo ON eo.org_id = o.org_id\n" +
                "LEFT JOIN sys_emp e ON e.emp_id = eo.emp_id\n" +
                "WHERE\n" +
                "	e.emp_id = '" + id + "' AND ll.description = 0 order by ll.cdate desc ";
        PageUtil<GridOwnerLog> logs = queryUtil.queryByPage(sql, GridOwnerLog.class, page, pageSize);
        List<GridOwnerLog> content = logs.getContent();
        for (GridOwnerLog gridOwnerLog : content) {
            if (StringUtils.isNotBlank(gridOwnerLog.getEspPath())) {
                gridOwnerLog.setEspPath(fileUtil.getShowUrl() + gridOwnerLog.getEspPath());

            } else {
                gridOwnerLog.setEspPath(null);
            }
        }
        return logs;
    }

    public List<SysEmpOrg> getOrgId(int id) {
        String sql = "SELECT * FROM sys_emp_org WHERE emp_id='" + id + "'";
        return queryUtil.query(sql, SysEmpOrg.class);
    }

    public List<GridOwner> getGridId(int id) {
        String sql = "SELECT * FROM grid_owner WHERE emp_id ='" + id + "'";
        return queryUtil.query(sql, GridOwner.class);
    }




}
